﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ltrFlashMessage.Text = "Select a project to start browsing bugs";
            if (Master.CurrentUserManagement.CurrentUser.Role == (int)UserManagement.Role.Admin)
            {
                var projectNames = from item in ApplicationGlobalVariables.KillDaBugsDatabase.Projects
                                   select new
                                   {
                                       item.Name,
                                       item.ProjectId
                                   };
                if (projectNames.Any())
                    foreach (var project in projectNames)
                        ddlProjectName.Items.Add(new ListItem(project.Name, project.ProjectId.ToString()));
                else
                    ddlProjectName.Items.Add(new ListItem("Please add a project first", "-1"));
            }
            else
            {
                var projectNames = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Projects
                                   where item.User == Master.CurrentUserManagement.CurrentUser
                                   select new
                                   {
                                       item.Project.Name,
                                       item.Project.ProjectId
                                   };
                if (projectNames.Any())
                    foreach (var project in projectNames)
                        ddlProjectName.Items.Add(new ListItem(project.Name, project.ProjectId.ToString()));
                else
                {
                    ddlProjectName.Items.Add(new ListItem("No Project assigned to you yet", "-1"));
                    ddlSort.Items.Clear();
                }
            }
        }
    }

    protected void ddlProjectName_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlSort_SelectedIndexChanged(sender, e);
        ltrFlashMessage.Text = "Current Project: " + ddlProjectName.SelectedItem.Text;
    }
    protected void ddlSort_SelectedIndexChanged(object sender, EventArgs e)
    {
        ltrFlashMessage.Text = "Bugs now sorting by " + ddlSort.SelectedItem.Text;
        switch (ddlSort.SelectedValue)
        {
            case "Type":
                lvView.Sort("Type", SortDirection.Ascending);
                break;
            case "ID":
                lvView.Sort("BugId", SortDirection.Ascending);
                break;
            case "Severity":
                lvView.Sort("Severity", SortDirection.Ascending);
                break;
            case "SeverityD":
                lvView.Sort("Severity", SortDirection.Descending);
                break;
            case "Reproducability":
                lvView.Sort("Reproducability", SortDirection.Ascending);
                break;
            case "ReproducabilityD":
                lvView.Sort("Reproducability", SortDirection.Descending);
                break;
            case "Status":
                lvView.Sort("Status", SortDirection.Ascending);
                break;
            case "StatusD":
                lvView.Sort("Status", SortDirection.Descending);
                break;
            case "Priority":
                lvView.Sort("Priority", SortDirection.Ascending);
                break;
            case "PriorityD":
                lvView.Sort("Priority", SortDirection.Descending);
                break;
            case "Modified":
                lvView.Sort("LastUpdatedDate", SortDirection.Ascending);
                break;
            case "ModifiedD":
                lvView.Sort("LastUpdatedDate", SortDirection.Descending);
                break;
            case "Version":
                lvView.Sort("BugFoundInVersion", SortDirection.Ascending);
                break;
            case "VersionD":
                lvView.Sort("BugFoundInVersion", SortDirection.Descending);
                break;
            default:
                break;
        }
    }
}